--lua script
--使用幻武时装物品

--#include "data\config\item\WeaponFationItem.lua" once
--#include "data\config\fation\FationFemaleConfig.lua" once
--#include "data\config\fation\FationMaleConfig.lua" once
--#include "data\config\fation\WeaponFancyConfig.lua" once
--#include "data\config\fation\FootPrintConfig.lua" once
--#include "data\config\fation\EssentialQiConfig.lua" once


function UseWeaponFationItem(sysarg,itemidx,itemPtr,ItemTable)
	local idx = ItemTable.fationType
    local ii = ItemTable.fationId
    local itemSex = ItemTable.sex
    local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
    if itemSex ~= -1 and sex ~= itemSex then
        Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation014 ,ttFlyTip )
        return 
    end
    if idx == 2 then
        local fation = Actor.getIntProperty(sysarg, PROP_ACTOR_FATION_MALE)
        local cfg = FationMaleCfg
        if sex == 1 then
            fation = Actor.getIntProperty(sysarg, PROP_ACTOR_FATION_FEMALE)
            cfg = FationFemaleCfg
        end
        local config = cfg[ii]
        if not config then
            return
        end
        --[[
        local circle = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)
        if config.circle and config.circle > circle then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation023 ,ttFlyTip )
            return
        end
        local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
        if config.level and config.level > level and circle < 1 then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation025 ,ttFlyTip )
            return
        end
        ]]
        local circle = 0
        if config.circle then
            circle = config.circle
        end
        local level = 0
        if config.level then
            level = config.level
        end
        if not Actor.checkActorLevel(sysarg, level, circle) then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation025 ,ttFlyTip )
            return
        end
        local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
        if config.vocation and config.vocation ~= 0 and config.vocation ~= vocation then
             Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation021 ,ttFlyTip )
            return
        end
        if System.getIntBit(fation, ii-1) == 1 then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation001 ,ttFlyTip )
            return
        end
        
        local logId, logStr = LogIds.UseFationLogId, Lang.LogTips.UseFation
		if Actor.removeItemByPtr(sysarg, itemPtr, 1, true, logStr, logId) > 0 then

            local ActorName = Actor.getName(sysarg)
            local fationName = config.name2[vocation]
            local noticeStr = string.format(Lang.ScriptTips.Fation009, ActorName, fationName, enLinkFationWnd)
            System.broadcastTipmsg(noticeStr, ttScreenCenter + ttChatWindow)

	        fation = System.setIntBit(fation, ii-1, true)

            AddFationColor(sysarg, config.value)

	        if sex == 0 then
	            Actor.setIntProperty(sysarg, PROP_ACTOR_FATION_MALE, fation)
	        else
	            Actor.setIntProperty(sysarg, PROP_ACTOR_FATION_FEMALE, fation)
	        end

            Actor.collectOperate(sysarg,0)
	    end
    elseif idx == 1 then
        local weaponFancy = Actor.getIntProperty(sysarg, PROP_ACTOR_WEAPON_FANCY)
        local cfg = WeaponFancyCfg
        local config = cfg[ii]
        if not config then
            return
        end
        local circle = 0
        if config.circle then
            circle = config.circle
        end
        local level = 0
        if config.level then
            level = config.level
        end
        if not Actor.checkActorLevel(sysarg, level, circle) then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation026 ,ttFlyTip )
            return
        end
        --[[
        local circle = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)
        if config.circle and config.circle > circle then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation024 ,ttFlyTip )
            return
        end
        local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
        if config.level and config.level > level and circle < 1 then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation026 ,ttFlyTip )
            return
        end
        ]]
        local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
        if config.vocation and config.vocation ~= 0 and config.vocation ~= vocation then
             Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation022 ,ttFlyTip )
            return
        end
        if System.getIntBit(weaponFancy, ii-1) == 1 then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation002 ,ttFlyTip )
            return
        end
       
        local logId, logStr = LogIds.UseWeaponFancyLogId, Lang.LogTips.UseWeaponFancy
        if Actor.removeItemByPtr(sysarg, itemPtr, 1, true, logStr, logId) > 0 then

            local ActorName = Actor.getName(sysarg)
            local fationName = config.name2[vocation]
            local noticeStr = string.format(Lang.ScriptTips.Fation009, ActorName, fationName, enLinkFationWnd)
            System.broadcastTipmsg(noticeStr, ttScreenCenter + ttChatWindow)

            weaponFancy = System.setIntBit(weaponFancy, ii-1, true)

            AddFationColor(sysarg, config.value)

            Actor.setIntProperty(sysarg, PROP_ACTOR_WEAPON_FANCY, weaponFancy)

            Actor.collectOperate(sysarg,0)
        end

    elseif idx == 3 then
        local footPrint = Actor.getIntProperty(sysarg, PROP_ACTOR_FOOTPRINT)
        local cfg = FootPrintCfg
        local config = cfg[ii]
        if not config then
            return
        end
        if System.getIntBit(footPrint, ii-1) == 1 then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation003 ,ttFlyTip )
            return
        end
        local logId, logStr = LogIds.UseFootPrintLogId, Lang.LogTips.UseFootPrint
        if Actor.removeItemByPtr(sysarg, itemPtr, 1, true, logStr, logId) > 0 then
            local ActorName = Actor.getName(sysarg)
            local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
            local fationName = config.name2[vocation]
            local noticeStr = string.format(Lang.ScriptTips.Fation009, ActorName, fationName, enLinkFationWnd)
            System.broadcastTipmsg(noticeStr, ttScreenCenter + ttChatWindow)

            footPrint = System.setIntBit(footPrint, ii-1, true)

            AddFationColor(sysarg, config.value)

            Actor.setIntProperty(sysarg, PROP_ACTOR_FOOTPRINT, footPrint)

            Actor.collectOperate(sysarg,0)
        end
    elseif idx == 4 then
        local essentialQi = Actor.getIntProperty(sysarg, PROP_ACTOR_ESSENTIAL_QI)
        local cfg = EssentialQiCfg
        local config = cfg[ii]
        if not config then
            return
        end
        if System.getIntBit(essentialQi, ii-1) == 1 then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation004 ,ttFlyTip )
            return
        end
        
        local logId, logStr = LogIds.UseEssentialQiLogId, Lang.LogTips.UseEssentialQi
        if Actor.removeItemByPtr(sysarg, itemPtr, 1, true, logStr, logId) > 0 then
            local ActorName = Actor.getName(sysarg)
            local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
            local fationName = config.name2[vocation]
            local noticeStr = string.format(Lang.ScriptTips.Fation009, ActorName, fationName, enLinkFationWnd)
            System.broadcastTipmsg(noticeStr, ttScreenCenter + ttChatWindow)

            essentialQi = System.setIntBit(essentialQi, ii-1, true)

            AddFationColor(sysarg, config.value)

            Actor.setIntProperty(sysarg, PROP_ACTOR_ESSENTIAL_QI, essentialQi)

            Actor.collectOperate(sysarg,0)
        end
    elseif idx == 5 then
        local swingFation = Actor.getIntProperty(sysarg, PROP_ACTOR_SWING_FATION)
        local cfg = SwingFationCfg
        local config = cfg[ii]
        if not config then
            return
        end
        if System.getIntBit(swingFation, ii-1) == 1 then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation010 ,ttFlyTip )
            return
        end

        local swingLevelId = Actor.getIntProperty(sysarg, PROP_ACTOR_SWINGID)
        if config.step > swingLevelId then
            Actor.sendTipmsg( sysarg, Lang.ScriptTips.Fation012 ,ttFlyTip )
            return 
        end
        
        local logId, logStr = LogIds.UseSwingFationLogId, Lang.LogTips.UseSwingFationLogId
        if Actor.removeItemByPtr(sysarg, itemPtr, 1, true, logStr, logId) > 0 then
            local ActorName = Actor.getName(sysarg)
            local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
            local fationName = config.name2[vocation]
            local noticeStr = string.format(Lang.ScriptTips.Fation009, ActorName, fationName, enLinkFationWnd)
            System.broadcastTipmsg(noticeStr, ttScreenCenter + ttChatWindow)

            swingFation = System.setIntBit(swingFation, ii-1, true)

            AddFationColor(sysarg, config.value)

            Actor.setIntProperty(sysarg, PROP_ACTOR_SWING_FATION, swingFation)

            Actor.collectOperate(sysarg,0)
        end
    end

    UseWeaponFationSub(sysarg, 1, idx, ii)
end

function UseWeaponFationItemInit(sysarg)
  	for i = 1,table.getn(WeaponFationItemCfg) do
		local x = WeaponFationItemCfg[i]
		GlobalItemFn[x.item_id] = { func = UseWeaponFationItem, params = x }
  	end
end

table.insert(InitFnTable,UseWeaponFationItemInit)